package csdn_2;

import java.io.*;

public class Copy_Buf {
    public static void main(String[] args) {
        String srcPath="D:\\javacode\\javacode\\IO_1\\happy.txt";//源路径
        String destPath="D:\\javacode\\javacode\\IO_1\\happy_buf.txt";//拷贝路径

        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;

        String dateLine=null;

        try{
            bufferedReader=new BufferedReader(new FileReader(srcPath));//创建包装流对象
            bufferedWriter=new BufferedWriter(new FileWriter(destPath));

            while((dateLine=bufferedReader.readLine())!=null){
                bufferedWriter.write(dateLine);//写入读取的每一行数据
                bufferedWriter.newLine();//换行
            }

            System.out.println("拷贝成功~");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭流, 这里注意，只需要关闭 BufferedWriter、BufferedReader ，因为底层会自动的去关闭节点流FileWriter、FileReader.
            try {
                if(bufferedReader!=null) {
                    bufferedReader.close();
                }
                if(bufferedWriter!=null){
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
